package com.yf.controller;

import com.yf.dto.Message;
import com.yf.dto.Page;
import com.yf.dto.Result;
import com.yf.entity.Department;
import com.yf.myException.ExceptionEnum;
import com.yf.myException.NewException;
import com.yf.service.DepartmentService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@CrossOrigin
@ResponseBody
@RequiresAuthentication
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 增加部门
     * @param department
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/add_department")
    public Result addDepartment(Department department){
        System.out.println(department);
        try {
            departmentService.addDepartment(department);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_ADD);
        }
    }

    /**
     * 删除部门
     * @param deptId
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/delete_department")
    public Result deleteDepartment(Integer deptId){
        System.out.println(deptId);
        try {
            departmentService.deleteDepartment(deptId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_DELETE);
        }
    }

    /**
     * 更新部门
     * @param department
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/update_department")
    public Result updateDepartment(Department department){
        System.out.println(department);
        try {
            departmentService.updateDepartment(department);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_UPDATE);
        }
    }

    /**
     * 通过ID获取部门信息
     * @param deptId
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/getDepartmentByDeptId")
    public Result getDepartmentByDeptId(Integer deptId){

        try {
            Department department = departmentService.getDepartmentByDeptId(deptId);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(department);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

    /**
     * 获取页面列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/getDepartmentPageJson")
    public Result getDepartmentPage(Integer pageNo,Integer pageSize){

        try {
            Page departmentPage = departmentService.getDepartmentPage(pageNo, pageSize);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(departmentPage);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }


}
